var request = require('superagent');
var API = require('./support/const');
var service = require('../../service/log');

/**
 * 任务管理
 */
exports.tasks = {
    list: function (req, res, next) {
        let header = {
            token: req.cookies.token
        };
        let params = req.body;

        request.get(API.tasks.list)
            .set(header)
            .query({msg: JSON.stringify(params)})
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'getList', 'msg': '获取任务列表'}, req);
                }
                res.json(JSON.parse(_res.text));
            });
    },

    detail: function (req, res, next) {
        let header = {
            token: req.cookies.token
        };
        let {id} = req.body;
        request.get(API.tasks.detail + id)
            .set(header)
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'getId', 'msg': '获取任务详情'}, req);
                }

                res.json(JSON.parse(_res.text));
            });

    },
    add:function(req,res,next){
        let header = {
            token: req.cookies.token
        };
        let params = req.body;
        request.post(API.tasks.add)
            .set(header)
            .query({msg: JSON.stringify(params)})
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'add', 'msg': '新增任务'}, req);
                }
                res.json(JSON.parse(_res.text));
            });

    },
    del:function(req,res,next){
        let header = {
            token: req.cookies.token
        };
        let id = req.body.id;
        request.delete(API.tasks.delete+id)
            .set(header)
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'add', 'msg': '新增任务'}, req);
                }
                res.json(JSON.parse(_res.text));
            });

    },
    uploadNotification:function(req,res,next){
        let header = {
            token: req.cookies.token
        };
        let params = req.body;
        request.post(API.tasks.upload_notification)
            .set(header)
            .query({msg: JSON.stringify(params)})
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'add', 'msg': '下发现场检查通知书'}, req);
                }
                res.json(JSON.parse(_res.text));
            });

    },

};
